<?php
// +----------------------------------------------------------------------
// | genadmin [ genadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\admin\model\user;

use app\admin\logic\finance\UserMoneyLogLogic;
use app\admin\logic\finance\UserScoreLogLogic;
use app\basic\BaseModel;
use support\utils\Random;

/**
 * 会员管理模型
 */
class UserMember extends BaseModel
{

    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 数据库表名称
     * @var string
     */
    protected $table = 'eb_user_member';


    /**
     * 获取头像
     * @param string $value
     * @param array  $data
     * @return string
     */
    public function getAvatarAttr($value, $data)
    {
        $value = $value ?: ($data['avatar'] ?? '/assets/img/avatar.jpg');
        return getUrlAttr($value);
    }
    
    
  

    /**
     * 更新前
     */
    public static function onBeforeUpdate($row)
    {
        $changed = $row->getChangedData();
         if (isset($changed['password'])) {
            if ($changed['password']) {
                $salt = Random::alnum();
                $row->password = getEncryptPassword($changed['password'], $salt);
                $row->salt = $salt;
            } else {
                unset($row->password);
            }
         }
         if (isset($changed['change_type'])) {
            $origin = self::where(['id'=>$changed['id']])->find();
            if ($changed['change_type'] == 'money' && (function_exists('bccomp') ? bccomp($changed['number'], $origin['money'], 2) !== 0 : (double)$changed['money'] !== (double)$origin['money'])) {
                $changed['number'] = $changed['change_status']=='add'?$changed['number']:-$changed['number'];
                $title = $changed['change_status']=='add'?'系统增加余额':'系统减少余额';
                UserMoneyLogLogic::createLog($changed['id'],$changed['number'],$origin['money'],bcadd($origin['money'],$changed['number'],2),$title,$changed['description']);
                $row->money = bcadd($origin['money'],$changed['number'],2);
            }
            if ($changed['change_type'] == 'score' && (int)$changed['number'] !== (int)$origin['score']) {
                $changed['number'] = $changed['change_status']=='add'?$changed['number']:-$changed['number'];
                $title = $changed['change_status']=='add'?'系统增加积分':'系统减少积分';
                UserScoreLogLogic::createLog($changed['id'],$changed['number'],$origin['money'],bcadd($origin['score'],$changed['number'],2),$title,$changed['description']);
                $row->score = bcadd($origin['score'],$changed['number']);
            }
         }
    }
    
    /**
     * 用户名 搜索
     */
    public function searchUsernameAttr($query, $value)
    {
        $query->where('username', 'like', '%'.$value.'%');
    }
    
    /**
     * 手机号
     */
    public function searchMobileAttr($query, $value)
    {
        $query->where('mobile', 'like', '%'.$value.'%');
    }

    
    /**
     * 昵称 搜索
     */
    public function searchNicknameAttr($query, $value)
    {
        $query->where('nickname', 'like', '%'.$value.'%');
    }


    public function getPrevtimeAttr($value, $data)
    {
        $value = $value ? $value : ($data['prevtime'] ?? "");
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getLogintimeAttr($value, $data)
    {
        $value = $value ? $value : ($data['logintime'] ?? "");
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getJointimeAttr($value, $data)
    {
        $value = $value ? $value : ($data['jointime'] ?? "");
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setPrevtimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setLogintimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setJointimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setBirthdayAttr($value)
    {
        return $value ? $value : null;
    }
}
